"""

"""
import requests
import time
import pandas as pd
import math
import random

user_agent_list = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
    'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
    'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
    'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
    'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
    'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
    "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Mobile Safari/537.36",
]

# 目标url
url = "https://mp.weixin.qq.com/cgi-bin/appmsg"
cookie = "ua_id=uNM5L3SIOgBFm1dnAAAAAElC5d3lverFvMBzWHUI00k=; wxuin=09624089179345; mm_lang=zh_CN; _clck=aaf2hh|1|fnz|0; sig=h01e7dfe7fced1e213308546fbb2a2936a73772cdc3f84cce2c8fc9717e9e5879e5ef3ca0e940e85d63; cert=Y88_vgdFUX8Fsp7_vSfgATgbvnoImuNy; uuid=4feb5cf61030781d729fd2963e161b21; rand_info=CAESIKY3gFCwlDDaHjONWr55kFx7gnOfRiybeYZXJH5KRH7I; slave_bizuin=3935637788; data_bizuin=3935637788; bizuin=3935637788; data_ticket=CcCCmWvMsUAZ7IKr24nNGbRdgIp6jC8d6B6fVXBIhQ61uqsaHMXCAQfy6hqt9Xcw; slave_sid=YnljSHBqU0FqeWdNX1VLVndLaEYzb2tCa0k0V2l6WFZYT3RBUTEwMlVWeUszN3lIbFNGTXZUOEZ3bVRBMm9tYlpBZ1NfUGNHVnlSdlhJS3FpaU1MZzdPWFRycFFiOWxDdVlTTGxSWWRldVo3aUt0U2ZucGEwSzVVMlYzbDF4Uzg5YktMek0xTlpsaVBreDZl; slave_user=gh_8bdd07c02cbc; xid=e45a3cdd7dbef6b07253be02aa14e088; _clsk=1d36rfk|1722565193494|4|1|mp.weixin.qq.com/weheat-agent/payload/record"

# 使用Cookie，跳过登陆操作

data = {
    "token": "315802527",
    "lang": "zh_CN",
    "f": "json",
    "ajax": "1",
    "action": "list_ex",
    "begin": "0",
    "count": "5",
    "query": "",
    "fakeid": "MjM5NzcxODEyMQ==",
    "type": "9",
}
headers = {
    "Cookie": cookie,
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Mobile Safari/537.36",

}
content_json = requests.get(url, headers=headers, params=data).json()
count = int(content_json["app_msg_cnt"])
print(count)
page = int(math.ceil(count / 5))
print(page)
content_list = []
# 功能：爬取IP存入ip_list列表

for i in range(page):
    data["begin"] = i * 5
    user_agent = random.choice(user_agent_list)
    headers = {
        "Cookie": cookie,
        "User-Agent": user_agent,

    }
    ip_headers = {
        'User-Agent': user_agent
    }
    # 使用get方法进行提交
    content_json = requests.get(url, headers=headers, params=data).json()
    # 返回了一个json，里面是每一页的数据
    for item in content_json["app_msg_list"]:
        # 提取每页文章的标题及对应的url
        items = []
        items.append(item["title"])
        items.append(item["link"])
        t = time.localtime(item["create_time"])
        items.append(time.strftime("%Y-%m-%d %H:%M:%S", t))
        content_list.append(items)
    print(i)
    # if (i > 0) and (i % 10 == 0):
    if i > 0:
        name = ['title', 'link', 'create_time']
        test = pd.DataFrame(columns=name, data=content_list)
        test.to_csv("url.csv", mode='a', encoding='utf-8')
        print("第" + str(i) + "次保存成功")
        content_list = []
        time.sleep(random.randint(60,90))
    else:
        time.sleep(random.randint(15,25))

name = ['title', 'link', 'create_time']
test = pd.DataFrame(columns=name, data=content_list)
test.to_csv("url.csv", mode='a', encoding='utf-8')
print("最后一次保存成功")
